package zsc.nine.onlinemall.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zsc.nine.onlinemall.entity.Product;
import zsc.nine.onlinemall.entity.User;
import zsc.nine.onlinemall.mapper.UserMapper;

import java.util.List;

/**
 * author: 莫远烙
 */
@Component
public class UserService {
    @Autowired
    UserMapper userMapper;

    //添加用户

    public boolean insertUser(User user) {
        try {
            return userMapper.insertUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //根据邮箱删除用户
    public boolean userDelete(String userEmail) {
        return userMapper.deleteUser(userEmail);
    }

    //更新用户信息
    public boolean updateUser(String userEmail, String userName, String userAddress) {
        return (userMapper.updateUserName(userEmail, userName)
                && userMapper.updateUserAddress(userEmail, userAddress));
    }

    //更新用户密码
    public boolean updatePassword(String oldEmail, String newEmail) {
        return userMapper.updatePassword(oldEmail, newEmail);
    }

    //根据邮箱查找用户
    public User findUserByEmail(String userEmail) {
        return userMapper.findByEmail(userEmail);
    }

    //展示所有用户
    public List<User> showAllUser() {
        return userMapper.show();
    }

    //根据名字模糊查询
    public List<User> findByLikeName(String likeName) {
        return userMapper.findByLikeName(likeName);
    }

    //根据地址模糊查询
    public List findByLikeAddress(String likeAddress) {
        return userMapper.findByLikeName(likeAddress);
    }

    /**
     * @param UserEmail: 用户Email
     * @Description: 用户Email存在验证服务方法
     * @return: 发送结果 true为存在 false为不存在
     */
    //根据邮箱判断该邮箱是否已存在
    //返回结果 true为存在 false为不存在
    public Boolean UserExist(String UserEmail) {
        return userMapper.findByEmail(UserEmail) != null;
    }


    public List<User> pagedFindById(String index, String pageSize) {
        return userMapper.pagedFindById(Integer.parseInt(index), Integer.parseInt(pageSize));
    }

    public String getLength() {
        return userMapper.getLength();
    }


}
